package com.dev.security.tools.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.dev.security.tools.beans.Dictionary;
import com.dev.security.tools.beans.DictionaryId;
import com.dev.security.tools.dao.DictionaryRepository;
import com.dev.security.tools.service.IDictionaryService;
import com.dev.security.util.Criteria;
import com.dev.security.util.DataGrid;

/**
 * @author 飘渺青衣
 * @see
 */
@Service
public class DictionaryService implements IDictionaryService {

	@Autowired
	private DictionaryRepository dictionaryRepository;
	
	/* (non-Javadoc)
	 * @see com.dev.security.tools.service.IDictionaryService#save(com.dev.security.tools.beans.Dictionary)
	 */
	@Override
	public void save(Dictionary dic) {
		this.dictionaryRepository.save(dic);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.tools.service.IDictionaryService#findDictionary(java.lang.String, java.lang.String)
	 */
	@Override
	public Dictionary findDictionary(String parent, String value) {
		return this.dictionaryRepository.findOne(new DictionaryId(parent, value));
	}

	/* (non-Javadoc)
	 * @see com.dev.security.tools.service.IDictionaryService#findDictionaries(java.lang.String)
	 */
	@Override
	public List<Dictionary> findDictionaries(String parent) {
		return this.dictionaryRepository.findAll(parent);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.tools.service.IDictionaryService#remove(java.lang.String, java.lang.String)
	 */
	@Override
	public void remove(String parent, String value) {
		this.dictionaryRepository.delete(new DictionaryId(parent, value));
	}

	/* (non-Javadoc)
	 * @see com.dev.security.tools.service.IDictionaryService#findPage(int, int)
	 */
	@Override
	public DataGrid<Dictionary> findPage(String label, int page, int size) {
		Criteria<Dictionary> criteria = new Criteria<Dictionary>();
		criteria.and().eq("id.parent", "");
		if(StringUtils.isNotEmpty(label)) {
			criteria.and().like(
					"label", new StringBuilder().append("%").append(label).append("%").toString());
		}
		Sort sort = new Sort(new Order(Direction.ASC, "sort"));
		Pageable pageRequest = new PageRequest(page > 0 ? page - 1 : 0, size, sort);
		Page<Dictionary> pager = this.dictionaryRepository.findAll(criteria.toSpecifications(), pageRequest);
		return new DataGrid<Dictionary>(pager);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.tools.service.IDictionaryService#remove(java.lang.String, java.lang.String[])
	 */
	@Override
	public long remove(String parent, String[] values) {
		return this.dictionaryRepository.delete(parent, values);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.tools.service.IDictionaryService#check(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean exist(String parent, String value) {
		return this.dictionaryRepository.count(parent, value) > 0;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.tools.service.IDictionaryService#exist(java.lang.String)
	 */
	@Override
	public boolean exist(String value) {
		return this.exist("", value);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.tools.service.IDictionaryService#remove(java.lang.String)
	 */
	@Override
	public void remove(String[] parents) {
		this.dictionaryRepository.delete(parents);
	}

}
